#include "StdAfx.h"
#include "glModel.h"


glModel::glModel(void)
{
	str=new char[255];
}


glModel::~glModel(void)
{
}

char* glModel::seeknumberfloat(char*str){
	int pos=0;
	char *number=new char[12];
	int k=0;
	while(true){
		if(str[pos]=='-'||(str[pos]>='0'&&str[pos]<='9')){
			if(str[pos+1]=='.'){
				if(str[pos+2]>='0'&&str[pos+2]<='9')
				{
					for(int i=0;i<8;i++)
						number[k+i]=str[pos+i];
					return number;
				}
			}else {
				number[k]=str[pos];
				k++;
			}
		}else if(str[pos]=='\0')
		{
			delete number;
			return NULL;
		}
		pos++;
	}				
}

char* glModel::seeknumberint(char*str){
	int pos=0;
	char *number=new char[12];
	int k=0;
	while(true){
		if(str[pos]=='-'||(str[pos]<='9'&&str[pos]>='0')){
			if(str[pos+1]=='.'){
				if(str[pos+2]>='0'&&str[pos+2]<='9')
				{
					return NULL;
				}
				else {
					number[k]=str[pos];
					return number;
				}
			}else if(str[pos+1]>='0'&&str[pos+1]<='9'){
				number[k]=str[pos];
				k++;
			}else {
				number[k]=str[pos];
				return  number;
			}
		}else if(str[pos]=='\0')
		{
			delete number;
			return NULL;
		}
		pos++;
	}
				
}
int glModel::ScanInt(ifstream &infile){
	while(!infile.eof() ) { 
		infile>>str;
		char* m=seeknumberint(str);
		if(m){ 
			int a=(int)atof(m);
			delete m;
			return a;
		}
	}
	return -1;
}
float glModel::ScanFloat(ifstream &infile){
	while(!infile.eof() ) { 
		infile>>str;
		char* m=seeknumberfloat(str);
		if(m){ 
			float a=(float)atof(m);
			delete m;
			return a;
		}
	}
	return -1.0f;
}
unsigned int glModel::ScanUnsignedInt(ifstream &infile){
	return (unsigned int)ScanInt(infile);
}
void glModel::LoadModel(){
	
	ifstream infile;
	infile.open(path_model);
	if(!infile){
		printf("Cannot open file %s",path_model);
	}
	m_iNumVertices=ScanInt(infile);
	m_aVertices = new Vertex[m_iNumVertices];

	for(int i=0;i< m_iNumVertices;i++){
		m_aVertices[i].position.x=ScanFloat(infile);
		m_aVertices[i].position.y=ScanFloat(infile);
		m_aVertices[i].position.z=ScanFloat(infile);

		m_aVertices[i].normal.x=ScanFloat(infile);
		m_aVertices[i].normal.y=ScanFloat(infile);
		m_aVertices[i].normal.z=ScanFloat(infile);

		m_aVertices[i].binormal.x=ScanFloat(infile);
		m_aVertices[i].binormal.y=ScanFloat(infile);
		m_aVertices[i].binormal.z=ScanFloat(infile);

		m_aVertices[i].tangent.x=ScanFloat(infile);
		m_aVertices[i].tangent.y=ScanFloat(infile);
		m_aVertices[i].tangent.z=ScanFloat(infile);

		m_aVertices[i].texcoord.x=ScanFloat(infile);
		m_aVertices[i].texcoord.y=ScanFloat(infile);
	}
	m_iNumIndices=ScanInt(infile);
	m_aIndices=new unsigned int[m_iNumIndices];
	m_iNumFaces=m_iNumIndices/3;
	for(int i=0;i<m_iNumFaces;i++){
		ScanInt(infile);
		m_aIndices[i*3]=ScanUnsignedInt(infile);
		m_aIndices[i*3+1]=ScanUnsignedInt(infile);
		m_aIndices[i*3+2]=ScanUnsignedInt(infile);
	}
	infile.close();
}

void glModel::CleanUp(){
	delete str;
	delete m_aIndices;
	delete m_aVertices;
	delete path_model;
}
